package com.lepu.app.fun.tool.szqx.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.core.lib.application.BaseFragment;
import com.eyzhs.app.R;
import com.lepu.app.fun.tool.szqx.GrowActivity;
import com.lepu.app.fun.tool.szqx.view.GrowCurveView;
import com.lepu.app.fun.tool.szqx.view.GrowDataView;

import java.util.ArrayList;

public class GrowFragment extends BaseFragment {

	private View mMainView = null;
	private boolean mInitDataSuccess = false;// 是否加载完数据

	private ArrayList<LinearLayout> mTabLayoutList;
	private ArrayList<ImageView> mTabPointList;
	public ArrayList<Fragment> mFragmentList;
	public ArrayList<View> mViewList;
	
	public LinearLayout mDataLayout;
	public GrowDataView mDataView;
	public GrowCurveView mCurveView;
	public int mType = 1;
	public int mViewIndex = 0;
	
	@SuppressLint("InflateParams")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mMainView = LayoutInflater.from(getActivity()).inflate(R.layout.tool_grow_fragment, null, false);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);

		ViewGroup p = (ViewGroup) mMainView.getParent();
		if (p != null) {
			p.removeAllViewsInLayout();
		}
		return mMainView;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();

		mInitDataSuccess = false;
	}

	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);

		if (isVisibleToUser) {
			if (!mInitDataSuccess) {

				mInitDataSuccess = true;
				init();
			}
		}
	}

	/*
	 * 初始化ui控件，不要初始化数据！！！！
	 * 
	 * 初始化控件，需要用mMainView.findViewById 不能使用findViewById，否则程序会闪退的
	 */
	private void init() {
        Bundle data = getArguments();
		
		mType = data.getInt("type");
		mViewIndex = data.getInt("index");
		
		mTabLayoutList = new ArrayList<LinearLayout>();
		mTabPointList = new ArrayList<ImageView>();
		
		mTabLayoutList.add( (LinearLayout) mMainView.findViewById(R.id.tabLayout1));
		mTabLayoutList.add((LinearLayout) mMainView.findViewById(R.id.tabLayout2));
		
		mTabPointList.add((ImageView) mMainView.findViewById(R.id.tabImage1));
		mTabPointList.add((ImageView) mMainView.findViewById(R.id.tabImage2));
		
		mDataLayout = (LinearLayout) mMainView.findViewById(R.id.dataLayout);
		
		mDataView = new GrowDataView(getActivity(),mType);
		mCurveView = new GrowCurveView(getActivity(), mType);
		
		mViewList = new ArrayList<View>();
		mViewList.add(mCurveView);
		mViewList.add(mDataView);
		
		//layout click
		for(int i=0; i<mTabLayoutList.size(); i++){
			mTabLayoutList.get(i).setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					for(int k=0; k<mTabLayoutList.size(); k++){
						if(mTabLayoutList.get(k).getId() == v.getId()){
							mTabLayoutList.get(k).setSelected(true);
							mTabPointList.get(k).setVisibility(View.VISIBLE);
							mDataLayout.removeAllViews();
							mDataLayout.addView(mViewList.get(k));
							
							if(GrowActivity.getInstance() != null){
								GrowActivity.getInstance().viewIndex = k;
							}
							
//							mViewPager.setCurrentItem(k,false);
						}else{
							mTabLayoutList.get(k).setSelected(false);
							mTabPointList.get(k).setVisibility(View.INVISIBLE);
							
						}
					}
				}
			});
		}

		mDataLayout.addView(mViewList.get(mViewIndex));
		mTabLayoutList.get(mViewIndex).setSelected(true);
		mTabPointList.get(mViewIndex).setVisibility(View.VISIBLE);
	}
	
}
